package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Task;
import com.example.springboot.service.TaskService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务列表前端操作接口
 **/
@RestController
@RequestMapping("/Task")
@Api(value = "任务管理", tags = "任务列表操作接口")
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加任务", notes = "创建任务")
    public Result add(@RequestBody Task Task) {
        taskService.save(Task);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除任务", notes = "根据id删除任务")
    public Result deleteById(@PathVariable Integer id) {
        taskService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "批量删除任务", notes = "根据id批量删除任务")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        taskService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改任务", notes = "修改任务")
    public Result updateById(@RequestBody Task Task) {
        taskService.updateById(Task);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "根据id查询任务", notes = "根据id查询任务")
    public Result selectById(@PathVariable Integer id) {
        Task Task = taskService.getById(id);
        return Result.success(Task);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有任务", notes = "查询所有任务")
    public Result selectAll(Task Task) {
        List<Task> list = taskService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @ApiOperation(value = "分页查询任务", notes = "分页查询任务")
    public Result selectPage(Task Task,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Task> page = taskService.selectPage(Task, pageNum, pageSize);
        return Result.success(page);
    }

}